Package

Source Code of Jerry

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Vector;

import ciberIF.*;

public class Jerry {

  ciberIF cif;
 
  public static void main (String[] args) {
 
    String host, robName;
    int pos;
    int arg;
   
    //Valores padr�o
    host = "localhost";
    robName = "Jerry";
    pos = 1;
   
    try {
      arg = 0;
      while (arg < args.length) {
        if(args[arg].equals("-pos")) {
          if(args.length > arg+1) {
            pos = Integer.valueOf(args[arg+1]).intValue();
            arg += 2;
          }
        }
        else if(args[arg].equals("-robname")) {
          if(args.length > arg+1) {
            robName = args[arg+1];
            arg += 2;
          }
        }
        else if(args[arg].equals("-host")) {
          if(args.length > arg+1) {
            host = args[arg+1];
            arg += 2;
          }
        }
        else throw new Exception();
      }
    }
    catch (Exception e) {
      print_usage();
      return;
    }
   
    Jerry client = new Jerry();
   
    client.robName = robName;
   
    client.cif.InitRobot(robName, pos, host);
   
    client.mainLoop();
 
  }
 
  Jerry() {
    cif = new ciberIF();
    beacon = new beaconMeasure();
   
    beaconToFollow = 0;
    ground = -1;
    state = 0;
  }
 
  public void mainLoop() {
    while (true) {
      cif.ReadSensors();
      decide();
    }
  }
 
  public void decide() {
   
    if(cif.IsObstacleReady(0))
        irSensor0 = cif.GetObstacleSensor(0);
      if(cif.IsObstacleReady(1))
        irSensor1 = cif.GetObstacleSensor(1);
      if(cif.IsObstacleReady(2))
        irSensor2 = cif.GetObstacleSensor(2);

      if(cif.IsCompassReady())
        compass = cif.GetCompassSensor();
      if(cif.IsGroundReady())
        ground = cif.GetGroundSensor();

      if(cif.IsBeaconReady(beaconToFollow))
        beacon = cif.GetBeaconSensor(beaconToFollow);

      x = cif.GetX();
      y = cif.GetY();
      dir = cif.GetDir();
    System.out.println(robName + ": compass = " + compass + ", obj_dir = " + obj_dir);
   
    if (ground == beaconToFollow){
      cif.Finish();
      teste = false;
      System.out.println("Cheguei ao fim");
    }
    else {
      switch (state) {
        case 0: // Andamento livre
          if( irSensor0 > 4.0 ){
            if( irSensor1 > irSensor2 ){
              cif.DriveMotors(0.1, -0.1);
              //System.out.println("Estado -> 1 por encontrar uma parede a 4.0");
              state =  1;
              obj_dir = compass;
            } else {
              cif.DriveMotors(-0.1, 0.1);
              //System.out.println("Estado -> 2 por encontrar uma parede a 4.0");
              state = 2;
              obj_dir = compass;
            }
          } else {
            if( irSensor0 > 1.0 ) {
              if( irSensor1 > irSensor2 ) {
                cif.DriveMotors(0.1, 0.0);
                //System.out.println("Estado -> 1 por encontrar uma parede a 1.0");
                state = 1;
                obj_dir = compass;
              } else {
                cif.DriveMotors( 0.0, 0.1 );
                //System.out.println("Estado -> 2 por encontrar uma parede a 1.0");
                state = 2;
                obj_dir = compass;
              }
            } else {
              if( irSensor1 > 4.0 || irSensor2 > 4.0 ){
                if ( irSensor1 > irSensor2 ){
                  cif.DriveMotors(0.1, -0.1);
                  //System.out.println("Estado -> 1 por encontrar uma parede ao lado perto");
                  state =  1;
                  obj_dir = compass;
                } else {
                  cif.DriveMotors(-0.1, 0.1);
                  //System.out.println("Estado -> 2 por encontrar uma parede ao lado perto");
                  state = 2;
                  obj_dir = compass;
                }
              } else {
                if( irSensor1 > 1.0 || irSensor2 > 1.0 ) {
                  if( irSensor1 > irSensor2 ) {
                    cif.DriveMotors(0.1, 0.0);
                    //System.out.println("Estado -> 1 por encontrar uma parede ao lado");
                    state = 1;
                    obj_dir = compass;
                  } else {
                    cif.DriveMotors( 0.0, 0.1 );
                    //System.out.println("Estado -> 2 por encontrar uma parede ao lado");
                    state = 2;
                    obj_dir = compass;
                  }
                } else {
                  if( beacon.beaconVisible && beacon.beaconDir > 20 ) {
                    cif.DriveMotors(0.0, 0.1);
                  } else {
                    if( beacon.beaconVisible && beacon.beaconDir < -20 ) {
                      cif.DriveMotors(0.1, 0.0);
                    } else {
                      cif.DriveMotors(0.1, 0.1);
                    }
                  }
                }
              }
            }
          }
          break;
        case 1: // Contornar obst�culos � sua esquerda
          if( compass < obj_dir + 5 && compass > obj_dir - 5 ){
            state = 0;
            //System.out.println("Estado -> 0 por estar virado para o objectivo");
          } else {
            if( beacon.beaconVisible && beacon.beaconDir < -20 ) {
              //System.out.println("Estado -> 0 por encontrar o queijo");
              state = 0;
              cif.DriveMotors(0.1, 0.0);
            } else {
              if( irSensor0 > 1.0 ) {
                if( irSensor1 > 4.0 ) {
                  cif.DriveMotors(0.1, 0.0);
                } else {
                  cif.DriveMotors(0.0, 0.1);
                }
              } else {
                if( irSensor1 < 2.0 ) {
                  cif.DriveMotors(-0.1, 0.1);
                } else {
                  cif.DriveMotors(0.1, 0.1);
                }
              }
            }
          }
          break;
        case 2: // Contornar obst�culos � sua direita
          if( compass < obj_dir + 10 && compass > obj_dir - 10 ){
            state = 0;
            //System.out.println("Estado -> 0 por estar virado para o objectivo");
          } else {
            if( beacon.beaconVisible && beacon.beaconDir > 20 ) {
              state = 0;
              //System.out.println("Estado -> 0 por encontrar o queijo");
              cif.DriveMotors(0.0, 0.1);
            } else {
              if( irSensor0 > 1.0 ) {
                if( irSensor2 > 4.0 ) {
                  cif.DriveMotors(0.0, 0.1);
                } else {
                  cif.DriveMotors(0.1, 0.0);
                }
              } else {
                if( irSensor2 < 2.0 ) {
                  cif.DriveMotors(0.1, -0.1);
                } else {
                  cif.DriveMotors(0.1, 0.1);
                }
              }
            }
          }
          break;
      }
    }
   
    /*
    if(teste){
      for(int i=0; i<5; i++)
        if(cif.NewMessageFrom(i))
          System.out.println("Message: From " + i + " to " + robName + " : \"" + cif.GetMessageFrom(i)+ "\"");

      System.out.println("State = " + state);
    }*/
    cif.Say(robName);
   
    if(cif.GetTime() % 2 == 0) {
      cif.RequestIRSensor(0);
      if(cif.GetTime() % 8 == 0 || beaconToFollow == cif.GetNumberOfBeacons())
        cif.RequestGroundSensor();
      else
        cif.RequestBeaconSensor(beaconToFollow);
    }
    else {
      cif.RequestCompassSensor();
      cif.RequestIRSensor(1);
      cif.RequestIRSensor(2);
    }
   
   
  }
 
  static void print_usage() {
             System.out.println("Usage: java jClient [-robname <robname>] [-pos <pos>] [-host <hostname>[:<port>]]");
    }

  private boolean teste = true;
  private int state;
  private double obj_dir = 0; //Direc��o objectivo
    private String robName;
    private double irSensor0, irSensor1, irSensor2, compass;
    private beaconMeasure beacon;
    private int    ground;
    private boolean collision;
    private double x,y,dir;

    private int beaconToFollow;
 
};
TOP

Related Classes of Jerry

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.